package com.lfz.demo.utils;

import com.lfz.demo.common.enums.CensusTimeEnum;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedTypes(CensusTimeEnum.class)
@MappedJdbcTypes(JdbcType.INTEGER)
public class CensusTimeEnumTypeHandler extends BaseTypeHandler<CensusTimeEnum> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, CensusTimeEnum parameter, JdbcType jdbcType) throws SQLException {
        ps.setInt(i, parameter.getCode());
    }

    @Override
    public CensusTimeEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
        int code = rs.getInt(columnName);
        return CensusTimeEnum.getByCode(code);
    }

    @Override
    public CensusTimeEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        int code = rs.getInt(columnIndex);
        return CensusTimeEnum.getByCode(code);
    }

    @Override
    public CensusTimeEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        int code = cs.getInt(columnIndex);
        return CensusTimeEnum.getByCode(code);
    }
}